<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title="物联信息"
               :modal="false"
               class="user-dialog add-or-updata"
               width="45%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="IP地址"
                          prop="ip">
              <el-input v-model="dataForm.ip"
                        maxlength="20"
                        show-word-limit
                        placeholder="IP地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口"
                          prop="port">
              <el-input v-model="dataForm.port"
                        maxlength="20"
                        show-word-limit
                        placeholder="端口号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12"
                  v-if="dataForm.deviceType === 6">
            <el-form-item label="用户名"
                          prop="username">
              <el-input v-model="dataForm.username"
                        maxlength="20"
                        show-word-limit
                        placeholder="用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12"
                  v-if="dataForm.deviceType === 7">
            <el-form-item label="机号"
                          prop="machineNo">
              <el-input v-model="dataForm.machineNo"
                        maxlength="20"
                        show-word-limit
                        placeholder="机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码"
                          prop="password">
              <el-input v-model="dataForm.password"
                        maxlength="20"
                        show-word-limit
                        placeholder="密码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12"
                  v-if="dataForm.deviceType === 6">
            <el-form-item label="协议"
                          prop="agreement">
              <el-select v-model="dataForm.agreement"
                         clearable
                         style="width:100%"
                         placeholder="请选择设备类型">
                <el-option v-for="item in agreementList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t("cancel") }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        deleteImgURL: '/system/oss/delete',
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: 'tb/device/insertDevice', // 添加或修改接口，API地址
        updateURL: 'tb/device/updateDevice', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      visible: false,
      dataForm: {
        deviceType: '',
        ip: '',
        port: '',
        username: '',
        machineNo: '',
        password: '',
        agreement: ''
      },
      agreementList: [{ id: 1, name: 'Modbus-TCP' },
        { id: 2, name: 'Modbus-RTU' },
        { id: 3, name: 'HTTP' }]
    }
  },
  components: { },
  mounted () {
  },
  methods: {
    init (data) {
      if (data) {
      }
      this.$nextTick(() => {
        if (data.id) {
          this.dataForm.id = data.id
          this.dataForm.agreement = data.agreement
          this.dataForm.deviceType = data.deviceType
          let url = `tb/device/getIOTInformation?id=` + this.dataForm.id
          this.$http.get(url).then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            if (res.data) {
              this.dataForm = res.data

              // 值被覆盖，重新赋值
              this.dataForm.id = data.id
              // this.dataForm.agreement = data.agreement
              this.dataForm.deviceType = data.deviceType
            } else if (res.data === null) {
              this.dataForm.ip = ''
              this.dataForm.port = ''
              this.dataForm.username = ''
              this.dataForm.machineNo = ''
              this.dataForm.password = ''
              this.dataForm.agreement = ''
            }
          })
        }
      })
      this.visible = true
    },
    closeDialog () {
    //   this.$refs.dataForm.resetFields()
      this.dataForm.name = ''
      this.dataForm.subsystem = ''
      this.dataForm.template = ''
      this.dataForm.type = ''
      this.dataForm.status = 0
      this.dataForm.marks = ''
      this.dataForm.url = []
      this.dataForm.model = ''
      this.dataForm.brand = ''
      this.dataForm.aid = ''
      this.dataForm.supplier = ''
      this.dataForm.linkMan = ''
      this.dataForm.link = ''
      this.dataForm.deviceType = ''
      this.dataForm.deviceId = ''
      this.dataForm.platform = 0
      this.dataForm.id = ''
      this.dataForm.ip = ''
      this.dataForm.port = ''
      this.dataForm.username = ''
      this.dataForm.machineNo = ''
      this.dataForm.password = ''
      this.dataForm.agreement = ''
      this.dataForm.energy = 1
      this.$emit('callBack')
      this.visible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            let obj = {}
            obj.data = {}
            obj.data.id = this.dataForm.id
            if (this.dataForm.deviceType === 6) { // 网关
              obj.data.gateWayInfo = {}
              obj.data.gateWayInfo.ip = this.dataForm.ip
              obj.data.gateWayInfo.port = this.dataForm.port
              obj.data.gateWayInfo.username = this.dataForm.username
              obj.data.gateWayInfo.password = this.dataForm.password
              obj.data.gateWayInfo.agreement = this.dataForm.agreement
              obj.data.gateWayInfo.creator = this.dataForm.creator
              obj.data.gateWayInfo.createDate = this.dataForm.createDate
            } else if (this.dataForm.deviceType === 7) { // 门禁考勤一体机
              obj.data.attendanceMachineInfo = {}
              obj.data.attendanceMachineInfo.ip = this.dataForm.ip
              obj.data.attendanceMachineInfo.port = this.dataForm.port
              obj.data.attendanceMachineInfo.machineNo = this.dataForm.machineNo
              obj.data.attendanceMachineInfo.password = this.dataForm.password
              obj.data.attendanceMachineInfo.creator = this.dataForm.creator
              obj.data.attendanceMachineInfo.createDate = this.dataForm.createDate
            }
            this.$http.put('tb/device/updateIOTInformation', obj).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message.success('保存成功')
              this.closeDialog()
            })
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style lang="scss" scoped>
</style>
